/*
 * CalendarDatePanel.java
 *
 * Created on 06 May 2004, 10:25
 */

package com.velocityme.client.gui.calendar;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author  Robert
 */
public class CalendarDatePanel extends javax.swing.JPanel {
    
    private DateFormat m_dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private CalendarListener m_listener = null;

    /** Creates new form CalendarDatePanel */
    public CalendarDatePanel() {
        initComponents();
    }
    
    public void setDate(Date p_date) {
        jTextFieldDate.setText(m_dateFormat.format(p_date));
        if (m_listener != null)
            m_listener.setSelectAction(m_dateFormat.format(p_date));
    }
    
    public Date getDate() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(m_dateFormat.parse(jTextFieldDate.getText()));
        return calendar.getTime();
    }
    
    public void registerListener(CalendarListener listener) {
        m_listener = listener;
    }
    
    public void setEnabled(boolean p_enabled) {
        jButton1.setEnabled(p_enabled);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jTextFieldDate = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jTextFieldDate.setEnabled(false);
        add(jTextFieldDate, new java.awt.GridBagConstraints());

        jButton1.setText("...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jButton1, gridBagConstraints);

    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        CalendarFrame cf = new CalendarFrame();
        cf.registerListener(jTextFieldDate);
        cf.registerListener(m_listener);
        cf.show();        
    }//GEN-LAST:event_jButton1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextFieldDate;
    // End of variables declaration//GEN-END:variables
    
}
